var strjsonobj = {
    "code": 0,
    "message": "normal",
    "data": {
        "2017/11/3 8:0AM": {
            "F33": [
                {
                    "coordinate": {
                        "x": 0,
                        "y": 0,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },
                {
                    "coordinate": {
                        "x": 20,
                        "y": 0,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },

                {
                    "coordinate": {
                        "x": 20,
                        "y": 40,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },

                {
                    "coordinate": {
                        "x": 27,
                        "y": 20,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },

                {
                    "coordinate": {
                        "x": 50,
                        "y": 30,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },
                {
                    "coordinate": {
                        "x": 128,
                        "y": 128,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },
                {
                    "coordinate": {
                        "x": 256,
                        "y": 0,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },
                {
                    "coordinate": {
                        "x": 0,
                        "y": 256,
                        "z": "NaN"
                    },
                    "weight": 273.3427499903065
                },
                {
                    "coordinate": {
                        "x": 256,
                        "y": 256,
                        "z": "NaN"
                    },
                    "weight": 193.17508358496585
                }
            ]
        }
    }
};
var strmytestjson = '{"code": 0,"data": {"2017/11/3 8:0AM":{"F4":[{"coordinate":{"x": 0.58252693060786,"y": 87.37123700010125},"weight":273.3427499903065}]}}}';
function myjsonparse() {
    //var myobj = JSON.parse(strjsonobj);
    //var code = strjsonobj.code;
    //console.log(code);
     var coordatas = [] ;
     for(var i in strjsonobj.data.ca.F4 )
     {
         coordatas.push([strjsonobj.data.ca.F4[i].coordinate.x,strjsonobj.data.ca.F4[i].coordinate.y,
             strjsonobj.data.ca.F4[i].weight]);
     }
     return coordatas;
}
//json字符串数据,转化成能绘图的位置信息
function heatDataFormJson(charjson,strfloor)
{
    var jsonobj = JSON.parse(charjson);
    var coorddatas = [];
    var floordata = jsonobj.data["2017/11/3 8:0AM"][strfloor];
    for(var i=0;i<floordata.length;++i)
    {
        coorddatas.push([floordata[i].coordinate.x,floordata[i].coordinate.y,floordata[i].weight]);
    }
    return coorddatas;
}
//从制定的json字符串数据获取所有楼层的数据，数据形式为楼层字符串和楼层热力图信息数组
function GetAllDataFromJson(charjson)
{
    var jsonobj = JSON.parse(charjson);
    var alldatas = [];
    var floors = jsonobj.data["2017/11/3 8:0AM"];
    for(var pfloor in floors)
    {
        var floorData ={
            floorname:'',
            floorheatdata:[]
        }
        floorData.floorname = pfloor;
        for(var j=0;j<floors[pfloor].length;++j)
        {
            floorData.floorheatdata.push([floors[pfloor][j].coordinate.x,floors[pfloor][j].coordinate.y,floors[pfloor][j].weight]);
        }
        alldatas.push(floorData);
    }
    return alldatas;
}
var themes = {
    default:{
        0.4: 'blue',
        0.6: 'cyan',
        0.7: 'lime',
        0.9: 'yellow',
        1.0: 'red'
    },
    yellow: {
        0.4: '#d0ba02',
        0.6: '#f9fc00',
        //0.7: 'lime',
        0.9: '#fcbe00',
        1.0: '#fc7100'
    },
    blue: {
        0.4: '#008b90',
        0.6: '#00f3fc',
        //0.7: 'lime',
        0.9: '#80c2ff',
        1.0: '#0078ff'
    }
};
//热力图的纹理信息
var g_heatmapTextcanvas = [];
var g_heatmapTexture = [];
//从指定的json字符串获取楼层数据然后生成一个纹理
function CreateTextureFromJson(charjson,strfloor)
{

    var heatmaptmp = new SimpleHeatmap();
    heatmaptmp.setCanvasSize(
        256, 256
    ).setAreaSize(
        10, 10
    ).min(20).max(27).alpha(true).gradient(
        themes.default
    ).radius(
        13, 13
    ).data(heatDataFormJson(charjson,strfloor)).draw(true);
    var texture = new THREE.Texture(heatmaptmp.canvas());
    return texture;
}
function CreateHeatmapPlane()
{
    var heatplane = new THREE.Mesh(new THREE.PlaneGeometry(30, 85), new THREE.MeshBasicMaterial({color: 0xffffff}));
    heatplane.position.y = 175;
    heatplane.position.z = 90;
    heatplane.position.x = 108;
    heatplane.rotation.x = -0.5*Math.PI;
    heatplane.rotation.z = 0.15*Math.PI;
    var myfloor = 'F33';
    //scene.add(screen);
    var texture = CreateTextureFromJson(JSON.stringify(strjsonobj),myfloor);
    //增加随机产生热力图数据
    var datas = [];
    for(var i = 0; i < 80; i++) {
        datas.push([Math.random() * 100 +10, Math.random() * 206+10, Math.random()*255]);
    }
    var heatmaptmp2 = new SimpleHeatmap();
    heatmaptmp2.setCanvasSize(
        128, 256
    ).setAreaSize(
        10, 10
    ).min(20).max(27).alpha(true).gradient(
        themes.default
    ).radius(
        8, 8
    ).data(datas).draw(true);
    var texture2 = new THREE.Texture(heatmaptmp2.canvas());
    texture2.needsUpdate = true;
    heatplane.material.map = texture2;
    heatplane.material.transparent = true;
    heatplane.material.side = THREE.DoubleSide;
    heatplane.material.opacity = 0.8;
    heatplane.material.depthTest = false;
    heatplane.material.depthWrite = false;
    heatplane.renderOrder = 100;
    return heatplane;
}

//在空间指定的位置创建平面：pos,方位 rotate:旋转, data：数据,sizexy:尺寸
var CreateHeatmapPlane = function (pos,rotate,data,sizexy,scene) {
    var heatplane = new THREE.Mesh(new THREE.PlaneGeometry(sizexy[0], sizexy[1]), new THREE.MeshBasicMaterial({color: 0xffffff}));
    [heatplane.position.x,heatplane.position.y,heatplane.position.z] = pos;
    [heatplane.rotation.x,heatplane.rotation.y,heatplane.rotation.z] = rotate;
    var heatmap = new SimpleHeatmap();
    heatmap.setCanvasSize(
        128, 128*Math.floor(sizexy[1]/sizexy[0])
    ).setAreaSize(
        10, 10
    ).min(20).max(27).alpha(true);
    if(floorHeatmapEvent.stats == 1){
        heatmap.gradient(themes.default);
    }else if(floorHeatmapEvent.stats == 2){
        heatmap.gradient(themes.yellow);    
    }else if(floorHeatmapEvent.stats == 3){
        heatmap.gradient(themes.blue);
    };
    heatmap.radius(
        8, 8
    ).data(data).draw(true);
    var texture = new THREE.Texture(heatmap.canvas());
    texture.needsUpdate = true;
    heatplane.material.map = texture;
    heatplane.material.transparent = true;
    heatplane.material.side = THREE.DoubleSide;
    heatplane.material.opacity = 0.8;
    heatplane.material.depthTest = false;
    heatplane.material.depthWrite = false;
    heatplane.renderOrder = 100;
    scene.add(heatplane);

    g_heatmapTextcanvas.push(heatmap);
    g_heatmapTexture.push(texture);
    //返回热力图，为了能够在函数外部对数据更新，让纹理热力图进行动态变化
    //return heatmap;
    return heatplane;
}
//隐藏创建的热力图平面接口
var deletheatplane = function(plane){
    if( typeof plane !== undefined){
        for(var ps = 0;ps<plane.length;++ps){
            //scene.remove(plane[ps]);
            plane[ps].visible = false;
        }
    }
    //plane = [];
}
//根据中心点的位置信息确定四种风格的数据进行热力图平面创建（三个中间层，一个基本层）
var CreateHeatmapFrompos = function(pos,scene,plane){
    if( typeof plane !== undefined){
        for(var ps = 0;ps<plane.length;++ps){
            scene.remove(plane[ps]);
        }
    }
    var datapos = [pos[1]+20,pos[2]-2,pos[0]-20];
    var datapos2 = [pos[1]-30,pos[2]-2,pos[0]-10];
    //datapos2[0] = datapos2[0]-60;
    var datarotate2 = [-0.5*Math.PI,0,0.05*Math.PI];
    //var dataorigin = [pos];

    var datarotate = [[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI]];//五种风格的平面旋转
    var datasizexy = [[38,85],[38,85],[38,85],[38/1.5,85/1.5],[10,40]];
    var dataheat = CreateRandomheatpoint();
    var dataheat2 = CreateRandomheatpoint();
    //根据位置信息进行判定当前要创建当前的热力图类型
    var i = -1;
    if( pos[0]> 108){
        return [];
    }else if(pos[0]>106){
        i = 1;
        datapos = [pos[1]+35,pos[2]-2,pos[0]-15];
        datapos2 = [pos[1]-30,pos[2]-2,pos[0]+10];
    }else if(pos[0]>105){
        if(pos[1]>83){
            i = 3;
            datapos = [pos[1]+20,pos[2]-2,pos[0]-15];
            datapos2 = [pos[1]-35,pos[2]-2,pos[0]];
        }else if(pos[1]>79){
            i=4;
            datapos = [pos[1]+20,pos[2]-2,pos[0]-20];
            datapos2 = [pos[1]-30,pos[2]-2,pos[0]-10];
            dataheat = CreateRandomheatpointxy(datasizexy[i]);
            dataheat2 = CreateRandomheatpointxy(datasizexy[i]);
        }else if(pos[1]>78){
            i=2;
            datapos = [pos[1]+35,pos[2]-2,pos[0]-15];
            datapos2 = [pos[1]-30,pos[2]-2,pos[0]+10];
        }
    }else if(pos[0]>86 && pos[0]<100){
        i = 0;
        datapos = [pos[1]+7,pos[2]-2,pos[0]+5];
        datapos2 = [pos[1]-60,pos[2]-2,pos[0]+30];
        //datarotate2 = datarotate[i];
        //datarotate2[2] =
    }
    if(i<0){
        return [];
    }
    g_heatmapTextcanvas = [];
    g_heatmapTexture = [];
    return [CreateHeatmapPlane(datapos,datarotate[i],dataheat,datasizexy[i],scene),CreateHeatmapPlane(datapos2,datarotate2,dataheat2,datasizexy[i],scene)];
}
//更新热力图的纹理数据,可以实现热力图动态变换
var updateHeatmapPlane = function (heatmaps,data) {
    for(var i = 0 ;i<heatmaps.length; ++i)
    {
        heatmaps[i].data(data[i]).draw();
    }
};

var CreateRandomheatpoint = function(){
    //增加随机产生热力图数据
    var datas = [];
    for(var i = 0; i < 40; i++) {
        datas.push([Math.random() * 80 +10, Math.random() * 190+10, Math.random()*255]);
    }
    return datas;
};
var CreateRandomheatpointxy = function(sizexy){
    //增加随机产生热力图数据
    var datas = [];
    for(var i = 0; i < 80; i++) {
        datas.push([Math.random() * 120 +2, Math.random() * (128*Math.floor(sizexy[1]/sizexy[0])-20)+10, Math.random()*255]);
    }
    return datas;
};
//创建多层楼层热力图
var CreateMultifloorPlane = function(scene)
{
    var heatplanes = [];
    var datapos = [[112,15,90],[112,35,90],[112,55,90],[112,75,90],[112,95,90],[112,115,90],[112,135,90],[112,155,90],[112,175,90],[112,195,90]];
    var dataroate = [[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI],[-0.5*Math.PI,0,0.15*Math.PI]];
    var datasizexy = [[38,85],[38,85],[38,85],[38,85],[38,85],[38,85],[38,85],[38,85],[38,85],[38,85]];
    var dataheat = [CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint(),CreateRandomheatpoint()];
    for(var i=0;i<datapos.length;++i)
    {
        heatplanes.push(CreateHeatmapPlane(datapos[i],dataroate[i],dataheat[i],datasizexy[i],scene));
        //CreateHeatmapPlane(datapos[i],dataroate[i],dataheat[i],datasizexy[i],scene);
    }
    return heatplanes;
}
var timeinterval = 0;
var lasttime = 0;
var updateheatTexture = function(){
    var curdate = new Date();
   timeinterval =curdate.getTime() - lasttime ;
   if(timeinterval < 10000){
       //console.log("not modify texture!");
       return;
   }
    for(var i= 0;i<g_heatmapTextcanvas.length;++i){
        if(g_heatmapTextcanvas[i]._data.length > 40){
            //g_heatmapTextcanvas[i].data(CreateRandomheatpointxy([10,40])).draw();
            //g_heatmapTexture[i].needsUpdate = true;
        }else{
            g_heatmapTextcanvas[i].data(CreateRandomheatpoint()).draw();
            g_heatmapTexture[i].needsUpdate = true;
        }
        lasttime = curdate.getTime();
        //console.log("modify texture!");
    }
};

var floorHeatmapEvent = {};
//根据状态与点选的楼层信息判定是否可以显示当前的热力图
//暂时只是取一组数据作为判定依据，数据利用g_contourData (map数据结构)
//key值只用g_contourKeys中的第一个数据进行
floorHeatmapEvent.isShowheat = function(status,keys,datasmap,pickfloor,keysIndex){
    if(status <1 || status > 3){
        return false;
    }
    var curindex;
    if(keysIndex<1){
        curindex = keys.length-1;
    }else{
        curindex = keysIndex-1;
    }
    if(keys.length > 0){
        if(datasmap.has(keys[curindex])){
            var mapset = datasmap.get(keys[curindex]);
            if(mapset.has(pickfloor)){
                return true;
            }
        }
    }
    return false;
};
floorHeatmapEvent.stats = 0;